var UPLOAD = (function () {
    parseResultP5 = function (result) {
        var regexp = /<li><h5>[^<]+/g;
        var test, tests = [];
        while ((test = regexp.exec(result)) != null) {
            tests.push(test);
        }
        var cases = tests.length, cnt = 0;
        if (cases == 0) {
            return {
                total: "<ul><li><p><b>代码未通过编译</b></p></li>"
                        + "<li><p>通过率 <b id='passgrade'>" + 0 + "</b><b>%</b></p></li>"
                        + "</ul>",
                detail: result
            }
        }
        else {
            for (var i = 0; i < cases; ++i) {
                if (tests[i][0].search("未通过") == -1) {
                    cnt = cnt + 1;
                }
            }
            return {
                total: "<ul><li><p>共 <b>" + cases + "</b>个测试点</p></li>"
                         + "<li><p>通过 <b>" + cnt + "</b>个测试点</p></li>"
                         + "<li><p>通过率 <b id='passgrade'>" + (cnt/cases) * 100 + "</b><b>%</b></p></li>"
                         + "</ul>",
                detail: result
            };
        }
    };

    // submit test file.
    fileUpload = function (form, action_url) {
        // Create the iframe...

        var iframe = document.createElement("iframe");
        iframe.setAttribute("id", "upload_iframe");
        iframe.setAttribute("name", "upload_iframe");
        iframe.setAttribute("width", "0");
        iframe.setAttribute("height", "0");
        iframe.setAttribute("border", "0");
        iframe.setAttribute("style", "width: 0; height: 0; border: none;");

        // Add to document...
        form.parentNode.appendChild(iframe);
        window.frames['upload_iframe'].name = "upload_iframe";

        iframeId = document.getElementById("upload_iframe");

        // Add event...
        var eventHandler = function () {

            if (iframeId.detachEvent) iframeId.detachEvent("onload", eventHandler);
            else iframeId.removeEventListener("load", eventHandler, false);

            // Message from server...
            if (iframeId.contentDocument) {
                content = iframeId.contentDocument.body.innerHTML;
            } else if (iframeId.contentWindow) {
                content = iframeId.contentWindow.document.body.innerHTML;
            } else if (iframeId.document) {
                content = iframeId.document.body.innerHTML;
            }

            var result = parseResultP5(content);
            document.getElementById('result_summary').innerHTML = result['total'];
            document.getElementById('result_detail').innerHTML = result['detail'];

            // Del the iframe...
            setTimeout('iframeId.parentNode.removeChild(iframeId)', 250);
        }

        if (iframeId.addEventListener) iframeId.addEventListener("load", eventHandler, true);
        if (iframeId.attachEvent) iframeId.attachEvent("onload", eventHandler);

        // Set properties of form...
        form.setAttribute("target", "upload_iframe");
        form.setAttribute("action", action_url);
        form.setAttribute("method", "post");
        form.setAttribute("enctype", "multipart/form-data");
        form.setAttribute("encoding", "multipart/form-data");

        // Submit the form...
        form.submit();

        document.getElementById('result_summary').innerHTML = "正在运行测试 ...";
        document.getElementById('result_detail').innerHTML = "";
    }

    var channel = Channel.build({
        window: window.parent,
        origin: "*",
        scope: "JSInput"
    });

    channel.bind("getGrade", getGrade);
    channel.bind("getState", getState);
    channel.bind("setState", setState);

    function getGrade() {
        return JSON.stringify({answer: $('#passgrade').html()});
    }

    function getState() {
        return JSON.stringify({answer: $('#passgrade').html()});
    }

    function setState() {
    }

    return {
        getState: getState,
        setState: setState,
        getGrade: getGrade
    };
}());
window.UPLOAD = UPLOAD;
